// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   StringUtil.java

package ui;

import java.util.Vector;
import javax.microedition.lcdui.Font;

public class StringUtil
{

    public StringUtil()
    {
    }

    public static String[] splitURL(String url)
        throws Exception
    {
        StringBuffer u = new StringBuffer(url);
        String result[] = new String[6];
        for(int i = 0; i <= 5; i++)
            result[i] = "";

        int index = url.indexOf(":");
        if(index > 0)
        {
            result[0] = url.substring(0, index);
            u.delete(0, index + 1);
        } else
        if(index == 0)
            throw new Exception("url format error - protocol");
        if(u.length() > 2 && u.charAt(0) == '/' && u.charAt(1) == '/')
        {
            u.delete(0, 2);
            int slash = u.toString().indexOf('/');
            if(slash < 0)
                slash = u.length();
            int colon = u.toString().indexOf(':');
            int endIndex = slash;
            if(colon >= 0)
            {
                if(colon > slash)
                    throw new Exception("url format error - port");
                endIndex = colon;
                result[2] = u.toString().substring(colon + 1, slash);
            }
            result[1] = u.toString().substring(0, endIndex);
            u.delete(0, slash);
        }
        if(u.length() > 0)
        {
            url = u.toString();
            int slash = url.lastIndexOf('/');
            if(slash > 0)
                result[3] = url.substring(0, slash);
            if(slash < url.length() - 1)
            {
                String fn = url.substring(slash + 1, url.length());
                int anchorIndex = fn.indexOf("#");
                if(anchorIndex >= 0)
                {
                    result[4] = fn.substring(0, anchorIndex);
                    result[5] = fn.substring(anchorIndex + 1);
                } else
                {
                    result[4] = fn;
                }
            }
        }
        return result;
    }

    public static String guessContentType(String url)
        throws Exception
    {
        String sURL[] = splitURL(url);
        String ext = "";
        String ct = "";
        int lastDot = sURL[4].lastIndexOf('.');
        if(lastDot >= 0)
            ext = sURL[4].substring(lastDot + 1).toLowerCase();
        if(ext.equals("mpg") || url.equals("avi"))
            ct = "video/mpeg";
        else
        if(ext.equals("mid") || ext.equals("kar"))
            ct = "audio/midi";
        else
        if(ext.equals("wav"))
            ct = "audio/x-wav";
        else
        if(ext.equals("jts"))
            ct = "audio/x-tone-seq";
        else
        if(ext.equals("txt"))
            ct = "audio/x-txt";
        else
        if(ext.equals("amr"))
            ct = "audio/amr";
        else
        if(ext.equals("awb"))
            ct = "audio/amr-wb";
        else
        if(ext.equals("gif"))
            ct = "image/gif";
        return ct;
    }

    private static int[] cumulativeWidth(Font font, String s)
    {
        int i = s.length();
        int ai[] = new int[i + 1];
        int total = 0;
        for(int k = 1; k <= i; k++)
        {
            char c1 = s.charAt(k - 1);
            total += ai[k] = font.charWidth(c1);
        }

        ai[0] = total;
        return ai;
    }

    public static String[] getArrayFitString(Font font, String s, int width)
    {
        if(font == null || s == null || width <= 0 || s.length() == 0)
            return new String[0];
        int cumwidth[] = cumulativeWidth(font, s);
        if(cumwidth[0] <= width)
            return (new String[] {
                s
            });
        Vector vector = new Vector();
        int k = s.length();
        int l = s.indexOf(' ');
        if(l >= 0)
        {
            int i1 = 0;
            int l2;
            for(int k1 = 1; k1 < cumwidth.length; k1 = (i1 = l2) + 1)
            {
                int i2 = 0;
                boolean flag1 = false;
                do
                {
                    if(k1 >= cumwidth.length || i2 + cumwidth[k1] > width)
                        break;
                    if(s.charAt(k1 - 1) == '\n')
                    {
                        flag1 = true;
                        k1++;
                        break;
                    }
                    i2 += cumwidth[k1++];
                } while(true);
                l2 = k1 - 1;
                String s2 = s.substring(i1, flag1 && l2 > i1 ? l2 - 1 : l2);
                for(; l2 < k && s.charAt(l2) == ' '; l2++);
                int i3;
                if(!flag1 && l2 == k1 - 1 && k1 < cumwidth.length && (i3 = s2.lastIndexOf(' ')) >= 0)
                {
                    l2 = i1 + i3;
                    s2 = s.substring(i1, l2);
                    l2++;
                }
                vector.addElement(s2);
            }

        }
        String as[] = new String[vector.size()];
        vector.copyInto(as);
        return as;
    }

    public static String[] split(String str, char x)
    {
        Vector v = new Vector();
        String str1 = new String();
        for(int i = 0; i < str.length(); i++)
            if(str.charAt(i) == x)
            {
                v.addElement(str1);
                str1 = new String();
            } else
            {
                str1 = str1 + str.charAt(i);
            }

        v.addElement(str1);
        String array[] = new String[v.size()];
        for(int i = 0; i < array.length; i++)
            array[i] = new String((String)v.elementAt(i));

        return array;
    }

    public static String getFitString(String string, Font font, int width)
    {
        int cumwidth[] = cumulativeWidth(font, string);
        if(cumwidth[0] <= width)
            return string;
        StringBuffer buffer = new StringBuffer();
        int w = 0;
        int last = -1;
        for(int i = 1; i <= cumwidth.length && w < width; i++)
        {
            if(string.charAt(i - 1) == '.' || string.charAt(i - 1) == ' ' || string.charAt(i - 1) == ',')
                last = i - 1;
            buffer.append(string.charAt(i - 1));
            w += cumwidth[i];
        }

        if(last > 0)
            buffer.setLength(last);
        else
        if(w >= width)
            buffer.setLength(buffer.length() - 1);
        return buffer.toString();
    }
}
